package com.xspace.android.xafcommon.view.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xspace.android.xafcommon.R;

/**
 * Created by Wubj on 2016/11/16 0016.
 */
public class UpdateDialog extends Dialog {

    private Context mContext;
    private TextView tvCache;
    private TextView content;
    private Button confirm;
    private Button ignore;
    private Button cancel;
    private LinearLayout forceUpdate;

    private ButtonClickListener confirmListener;
    private ButtonClickListener ignoreListener;
    private ButtonClickListener cancelListener;

    public UpdateDialog(Context context, int theme) {
        super(context, theme);
        mContext = context;
        this.setContentView(R.layout.dialog_update);
        this.setCanceledOnTouchOutside(false);
        findView();
        setListener();
    }

    /**
     * 设置内容
     *
     * @param messageContent 内容
     */
    private void setMessageContent(String messageContent) {
        content.setText(messageContent);
    }

    /**
     * 设置确认键内容
     *
     * @param confirmText
     */
    private void setConfirmText(String confirmText) {
        confirm.setText(confirmText);
    }

    /**
     * 设置取消键内容
     *
     * @param cancelText
     */
    private void setCancelText(String cancelText) {
        cancel.setText(cancelText);
    }

    /**
     * 设置强制更新
     */
    private void setForce() {
        setCancelBtGone();
        setIgnoreBtGone();
    }

    /**
     * 设置取消按钮不可见
     */
    private void setCancelBtGone() {
        cancel.setVisibility(View.GONE);
    }

    /**
     * 设置忽略按钮不可见
     */
    private void setIgnoreBtGone() {
        ignore.setVisibility(View.GONE);
    }

    /**
     * 当发现了本地的安装包时显示的内容
     */
    private void setCacheTvShow() {
        tvCache.setVisibility(View.VISIBLE);
    }

    /**
     * 设置监听事件
     */
    private void setListener() {
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (confirmListener != null) {
                    confirmListener.buttonClick(v);
                }
                dismiss();
            }
        });
        ignore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ignoreListener != null) {
                    ignoreListener.buttonClick(v);
                }
                dismiss();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cancelListener != null) {
                    cancelListener.buttonClick(v);
                }
                dismiss();
            }
        });
    }

    private void setIgnoreListener(ButtonClickListener ignoreListener) {
        this.ignoreListener = ignoreListener;
    }

    private void setConfirmListener(ButtonClickListener confirmListener) {
        this.confirmListener = confirmListener;
    }

    private void setCancelListener(ButtonClickListener cancelListener) {
        this.cancelListener = cancelListener;
    }


    public static class Builder {
        UpdateDialog mUpdateDialog;

        /**
         * Constructor using a context for this builder and the {@link AlertDialog} it creates.
         */
        public Builder(Context context, int theme) {
            mUpdateDialog = new UpdateDialog(context, theme);
        }

        public Builder(Context context) {
            this(context, R.style.CustomDialog);
        }

        public Builder(Context context, String message, String confirmText, String cancelText) {
            this(context, R.style.CustomDialog);
            this.setContentMsg(message).setConfirmText(confirmText).setCancelText(cancelText);
        }

        public Builder setContentMsg(String message) {
            mUpdateDialog.setMessageContent(message);
            return this;
        }

        public Builder setConfirmText(String confirmText) {
            mUpdateDialog.setConfirmText(confirmText);
            return this;
        }

        public Builder setCancelText(String cancelText) {
            mUpdateDialog.setCancelText(cancelText);
            return this;
        }

        public Builder setConfirmListener(ButtonClickListener listener) {
            mUpdateDialog.setConfirmListener(listener);
            return this;
        }

        public Builder setIgnoreListener(ButtonClickListener listener) {
            mUpdateDialog.setIgnoreListener(listener);
            return this;
        }

        public Builder setCancelListener(ButtonClickListener listener) {
            mUpdateDialog.setCancelListener(listener);
            return this;
        }

        public Builder setForceUpdate(boolean isForce) {
            if (isForce) {
                mUpdateDialog.setForce();
            }
            return this;
        }

        public Builder setIgnoreGone(Boolean isGone) {
            if (isGone) {
                mUpdateDialog.setIgnoreBtGone();
            }
            return this;
        }

        public Builder isCache(Boolean isCache) {
            if (isCache) {
                mUpdateDialog.setCacheTvShow();
//                mUpdateDialog.setIgnoreBtGone();
            }
            return this;
        }

        public void show() {
            mUpdateDialog.show();
        }
    }

    private void findView() {
        tvCache = (TextView) findViewById(R.id.tv_dialog_cache);
        content = (TextView) findViewById(R.id.tv_dialog_text);
        forceUpdate = (LinearLayout) findViewById(R.id.ll_dialog_force);

        confirm = (Button) findViewById(R.id.bt_dialog_confirm);
        ignore = (Button) findViewById(R.id.bt_dialog_ignore);
        cancel = (Button) findViewById(R.id.bt_dialog_cancel);
    }
}
